package demo3;

import java.util.concurrent.TimeUnit;

public class SynContainer {
    Chicken[] chickens = new Chicken[10];

    int count = 0;

    public synchronized void pushTo(Chicken chicken){
        if(chickens.length == count){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }

        }
        chickens[count] = chicken;
        System.out.println("生产了"+chicken.getId()+"鸡腿");
        count++;
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        notifyAll();
    }

    public synchronized Chicken popTo(){
        if(count == 0){
            try {
                wait();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
        count--;
        Chicken c = chickens[count];
        System.out.println("消费了"+c.getId()+"鸡腿");
        notifyAll();
        return c;
    }


}
